package com.sikeda.imagecomposetool;

import android.app.Application;
import android.graphics.Color;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.sikeda.imagecomposetool.utils.UILImageLoader;

import java.util.List;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ImageLoader;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by Administrator on 2016/12/12.
 */

public class AppContext extends Application {

    private static AppContext instance;

    public static AppContext getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initImageLoader();
        initImageSelecter();
    }

    /**
     * ImageLoader 初始化操作
     */
    private void initImageLoader() {

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getInstance());
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config.build());

/*        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(instance)
                .writeDebugLogs()//打开log打印图片加载信息
                .build();
        ImageLoader.getInstance().init(configuration);*/
    }


    /**
     * 配置图片选择器
     */
    private ThemeConfig theme;

    void initImageSelecter() {
        //设置主题
        //ThemeConfig.CYAN
        theme = new ThemeConfig.Builder()
                .setTitleBarBgColor(Color.parseColor("#FF4081"))
                .setPreviewBg(getResources().getDrawable(R.drawable.shape_selected_im_bg))//设置预览页背景
//                .setIconFab(R.mipmap.ic_launcher)//设置Floating按钮icon
                .setFabNornalColor(Color.parseColor("#FF4081"))//设置Floating按钮Nornal状态颜色
                .setFabPressedColor(Color.parseColor("#FF4081"))//设置Floating按钮Pressed状态颜色
                .setCheckSelectedColor(Color.parseColor("#FF4081"))//选择框选中颜色
                .build();
        setGalleryFinalConfig(null);
    }

    public void setGalleryFinalConfig(List<PhotoInfo> list) {
        //配置imageloader
        ImageLoader imageloader = new UILImageLoader();
        CoreConfig coreConfig = new CoreConfig.Builder(getInstance(), imageloader, theme)
//                .setDebug(BuildConfig.DEBUG)
                .setFunctionConfig(getFunctionConfig(list, 100))
//                .setNoAnimcation(true)//关闭动画
                .build();
        GalleryFinal.init(coreConfig);
    }

    public FunctionConfig getFunctionConfig(List<PhotoInfo> list, int maxNum) {
        if (list != null) {
            Log.i("info", "setSelected" + list.toString());
        }
        //配置功能
//        FunctionConfig functionConfig;
        return new FunctionConfig.Builder()
                .setEnableCamera(false)
                .setEnableEdit(false)
                .setEnableCrop(false)
                .setEnableRotate(false)//旋转
                .setSelected(list)
                .setCropSquare(true)
                .setEnablePreview(true)
                .setMutiSelectMaxSize(maxNum)
                .setEnablePreview(true)
                .build();
    }


}
